<?php
/**
 * 新闻动态
 * Created by PhpStorm.
 * User: xsh
 * Date: 2018/4/12  10:06
 */
namespace app\pc\Controller;
use think\Controller;

class Mechanism extends Base
{
    /*
    *	构造函数
    */
    function __construct(){
        parent::__construct();
    }

    //首页
    public function index(){
        $mid = input('mid');
        if(empty($mid)){
            $mechanism_category = db('mechanism_category')->field('name,id')->order('orders desc')->find();
            $mid = $mechanism_category['id'];
        }
        $pageParam = ['query' =>[]];
        $pageParam['query']['mid'] = $mid;

        //加入我们
        $mechanism = db('mechanism')->where(['mid'=>$mid])->order('orders desc')->paginate(9, false, $pageParam)->each(function($item,$key){
            $item['img'] = IMG_PATH.$item['img'];
            return $item;
        });

        //加入我们
        $mechanism_top = db('mechanism')->order('orders desc')->limit('0,8')->select();

        foreach ($mechanism_top as $key => $value) {
            $mechanism_top[$key]['img'] = IMG_PATH.$mechanism_top[$key]['img'];
        }

        //分类
        $mechanism_category_result = db('mechanism_category')->order('orders desc')->select();
        
        $this->assign('mechanism', $mechanism);
        $this->assign('mechanism_category_result',$mechanism_category_result);
        $this->assign('mechanism_top',$mechanism_top);
        $this->assign('mid',$mid);
        return $this->fetch('index');
    }
}